package cn.hahah.module.blog.controller.admin.articletag;

import cn.hahah.module.blog.dal.dataobject.article.ArticleDO;
import cn.hahah.module.blog.dal.dataobject.tag.TagDO;
import cn.hahah.module.blog.service.article.ArticleService;
import cn.hahah.module.blog.service.tag.TagService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.articletag.vo.*;
import cn.hahah.module.blog.dal.dataobject.articletag.ArticleTagDO;
import cn.hahah.module.blog.service.articletag.ArticleTagService;

@Tag(name = "管理后台 - 文章标签")
@RestController
@RequestMapping("/blog/article-tag")
@Validated
public class ArticleTagController {

    @Resource
    private ArticleTagService articleTagService;

    @Resource
    private ArticleService articleService;

    @Resource
    private TagService tagService;

    @PostMapping("/create")
    @Operation(summary = "创建文章标签")
    @PreAuthorize("@ss.hasPermission('blog:article-tag:create')")
    public CommonResult<Long> createArticleTag(@Valid @RequestBody ArticleTagSaveReqVO createReqVO) {
        return success(articleTagService.createArticleTag(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文章标签")
    @PreAuthorize("@ss.hasPermission('blog:article-tag:update')")
    public CommonResult<Boolean> updateArticleTag(@Valid @RequestBody ArticleTagSaveReqVO updateReqVO) {
        articleTagService.updateArticleTag(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文章标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:article-tag:delete')")
    public CommonResult<Boolean> deleteArticleTag(@RequestParam("id") Long id) {
        articleTagService.deleteArticleTag(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文章标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:article-tag:query')")
    public CommonResult<ArticleTagRespVO> getArticleTag(@RequestParam("id") Long id) {
        ArticleTagDO articleTag = articleTagService.getArticleTag(id);
        return success(BeanUtils.toBean(articleTag, ArticleTagRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文章标签分页")
    @PreAuthorize("@ss.hasPermission('blog:article-tag:query')")
    public CommonResult<PageResult<ArticleTagRespVO>> getArticleTagPage(@Valid ArticleTagPageReqVO pageReqVO) {
        PageResult<ArticleTagDO> pageResult = articleTagService.getArticleTagPage(pageReqVO);
        PageResult<ArticleTagRespVO> result = BeanUtils.toBean(pageResult, ArticleTagRespVO.class);

        for (ArticleTagRespVO articleTagRespVO : result.getList()) {
            ArticleDO articleDO = articleService.getArticle(articleTagRespVO.getArticleId());
            TagDO tagDO = tagService.getTag(articleTagRespVO.getTagId());

            if(articleDO != null){
                articleTagRespVO.setArticleName(articleDO.getTitle());
            }
            if(tagDO != null){
                articleTagRespVO.setTagName(tagDO.getTname());
            }
        }
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文章标签 Excel")
    @PreAuthorize("@ss.hasPermission('blog:article-tag:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportArticleTagExcel(@Valid ArticleTagPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ArticleTagDO> list = articleTagService.getArticleTagPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文章标签.xls", "数据", ArticleTagRespVO.class,
                        BeanUtils.toBean(list, ArticleTagRespVO.class));
    }

}